import type { Product } from "@/app/api/products/product";

export async function GET(request: Request) {

  const { searchParams } = new URL(request.url);
  const delay = searchParams.get('delay');
  if (delay) {
    await new Promise((resolve) => setTimeout(resolve, Number(delay)));
  }

  const id = searchParams.get('id');
  if (id) {
    let product = data.find((product) => product.id === id);

    const fields = searchParams.get('fields');
    if (product && fields) {
      product = fields.split(',').reduce((acc, field) => {
        // @ts-ignore
        acc[field] = product[field];

        return acc;
      }, {} as Product);
    }
    return new Response(JSON.stringify(product ?? null), {
      status: 200,
      headers: {
        'content-type': 'application/json',
      },
    });
  }

  const filter = searchParams.get('filter');
  const products = filter
    ? data.filter((product) => product.id !== filter)
    : data;

  return new Response(JSON.stringify(products), {
    status: 200,
    headers: {
      'content-type': 'application/json',
    },
  });
}

const data: Product[] = [
  {
    id: '1',
    stock: 2,
    rating: 5,
    name: 'Donec sit elit',
    description:
      'Morbi eu ullamcorper urna, a condimentum massa. In fermentum ante non turpis cursus fringilla. Praesent neque eros, gravida vel ante sed, vehicula elementum orci. Sed eu ipsum eget enim mattis mollis.',
    price: {
      amount: 4200,
      currency: { code: 'USD', base: 10, exponent: 2 },
      scale: 2,
    },
    isBestSeller: false,
    leadTime: 2,
    discount: { percent: 90, expires: 2 },
    image: 'eniko-kis-KsLPTsYaqIQ-unsplash.jpg',
    imageBlur:
      '',
  },
  {
    id: '2',
    stock: 5,
    rating: 4,
    name: 'Fusce commodo porta posuere',
    description:
      'Morbi eu ullamcorper urna, a condimentum massa. In fermentum ante non turpis cursus fringilla. Praesent neque eros, gravida vel ante sed, vehicula elementum orci. Sed eu ipsum eget enim mattis mollis.',
    price: {
      amount: 4600,
      currency: { code: 'USD', base: 10, exponent: 2 },
      scale: 2,
    },
    isBestSeller: false,
    leadTime: 1,
    image: 'patrick-OIFgeLnjwrM-unsplash.jpg',
    imageBlur:
      '',
  },
  {
    id: '3',
    stock: 3,
    rating: 3,
    name: 'Praesent tincidunt lectus',
    description:
      'Morbi eu ullamcorper urna, a condimentum massa. In fermentum ante non turpis cursus fringilla. Praesent neque eros, gravida vel ante sed, vehicula elementum orci. Sed eu ipsum eget enim mattis mollis.',
    price: {
      amount: 29200,
      currency: { code: 'USD', base: 10, exponent: 2 },
      scale: 2,
    },
    discount: { percent: 70, expires: 7 },
    isBestSeller: true,
    leadTime: 2,
    image: 'yoann-siloine-_T4w3JDm6ug-unsplash.jpg',
    imageBlur:
      '',
  },
  {
    id: '4',
    stock: 2,
    rating: 5,
    name: 'Morbi at viverra turpis',
    description:
      'Morbi eu ullamcorper urna, a condimentum massa. In fermentum ante non turpis cursus fringilla. Praesent neque eros, gravida vel ante sed, vehicula elementum orci. Sed eu ipsum eget enim mattis mollis.',
    price: {
      amount: 21200,
      currency: { code: 'USD', base: 10, exponent: 2 },
      scale: 2,
    },
    isBestSeller: false,
    leadTime: 2,
    image: 'alexander-andrews-brAkTCdnhW8-unsplash.jpg',
    imageBlur:
      '',
  },
  {
    id: '5',
    stock: 1,
    rating: 4,
    name: 'Maecenas interdum',
    description:
      'Morbi eu ullamcorper urna, a condimentum massa. In fermentum ante non turpis cursus fringilla. Praesent neque eros, gravida vel ante sed, vehicula elementum orci. Sed eu ipsum eget enim mattis mollis.',
    price: {
      amount: 28700,
      currency: { code: 'USD', base: 10, exponent: 2 },
      scale: 2,
    },
    isBestSeller: false,
    leadTime: 4,
    image: 'guillaume-coupy-6HuoHgK7FN8-unsplash.jpg',
    imageBlur:
      '',
  },
];
